---
layout: "default"
title: "UnsafeMutablePointer"
description: "Swift documentation for 'UnsafeMutablePointer': A pointer to an object of type T.  This type provides no automated."
keywords: "UnsafeMutablePointer,struct,swift,documentation,advancedBy,alloc,assignBackwardFrom,assignFrom,dealloc,destroy,destroy,distanceTo,encode,getMirror,initialize,initializeFrom,initializeFrom,move,moveAssignFrom,moveInitializeBackwardFrom,moveInitializeFrom,predecessor,put,successor,debugDescription,hashValue,memory"
root: "/v1.2"
---

<div class="intro-declaration"><code class="language-swift">struct UnsafeMutablePointer&lt;T&gt;</code></div>

<div class="discussion comment">
    <p>A pointer to an object of type <code>T</code>.  This type provides no automated
memory management, and therefore the user must take care to allocate
and free memory appropriately.</p>

<p>The pointer can be in one of the following states:</p>

<ul><li><p>memory is not allocated (for example, pointer is null, or memory has
been deallocated previously);</p></li><li><p>memory is allocated, but value has not been initialized;</p></li><li><p>memory is allocated and value is initialized.</p></li></ul>
</div>

<table class="standard">
<tr>
<th id="inheritance">Inheritance</th>
<td>
<code class="inherits">BidirectionalIndexType, CVarArgType, Comparable, DebugPrintable, Equatable, ForwardIndexType, Hashable, NilLiteralConvertible, RandomAccessIndexType, Reflectable, SinkType, Strideable, _BidirectionalIndexType, _Comparable, _ForwardIndexType, _Incrementable, _PointerType, _RandomAccessIndexType, _Strideable</code>
<span class="viz"><a href="hierarchy/">View Protocol Hierarchy &rarr;</a></span>
</td>
</tr>



<tr>
<th>Import</th>
<td><code class="language-swift">import Swift</code></td>
</tr>

</table>


<h3>Initializers</h3>
<div class="declaration" id="init">
<a class="toggle-link" data-toggle="collapse" href="#comment-init">init()</a><div class="comment collapse" id="comment-init"><div class="p">
    <p>Construct a null pointer.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init()</code>

    </div></div>
</div>
<div class="declaration" id="init_-copaquepointer">
<a class="toggle-link" data-toggle="collapse" href="#comment-init_-copaquepointer">init(<wbr>_: COpaquePointer)</a><div class="comment collapse" id="comment-init_-copaquepointer"><div class="p">
    <p>Convert from an opaque C pointer to a typed pointer</p>

<p>This is a fundamentally unsafe conversion.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init(_ other: COpaquePointer)</code>

    </div></div>
</div>
<div class="declaration" id="init-u_-unsafemutablepointer-u">
<a class="toggle-link" data-toggle="collapse" href="#comment-init-u_-unsafemutablepointer-u">init&lt;U&gt;(<wbr>_: UnsafeMutablePointer&lt;U&gt;)</a><div class="comment collapse" id="comment-init-u_-unsafemutablepointer-u"><div class="p">
    <p>Convert from an UnsafeMutablePointer of a different type.</p>

<p>This is a fundamentally unsafe conversion.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init&lt;U&gt;(_ from: UnsafeMutablePointer&lt;U&gt;)</code>

    </div></div>
</div>
<div class="declaration" id="init-u_-unsafepointer-u">
<a class="toggle-link" data-toggle="collapse" href="#comment-init-u_-unsafepointer-u">init&lt;U&gt;(<wbr>_: UnsafePointer&lt;U&gt;)</a><div class="comment collapse" id="comment-init-u_-unsafepointer-u"><div class="p">
    <p>Convert from a UnsafePointer of a different type.</p>

<p>This is a fundamentally unsafe conversion.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init&lt;U&gt;(_ from: UnsafePointer&lt;U&gt;)</code>

    </div></div>
</div>
<div class="declaration" id="init-bitpattern_-uword">
<a class="toggle-link" data-toggle="collapse" href="#comment-init-bitpattern_-uword">init(<wbr>bitPattern: UWord)</a><div class="comment collapse" id="comment-init-bitpattern_-uword"><div class="p">
    <p>Construct an <code>UnsafeMutablePointer</code> from a given address in memory.</p>

<p>This is a fundamentally unsafe conversion.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init(bitPattern: UWord)</code>

    </div></div>
</div>
<div class="declaration" id="init-bitpattern_-word">
<a class="toggle-link" data-toggle="collapse" href="#comment-init-bitpattern_-word">init(<wbr>bitPattern: Word)</a><div class="comment collapse" id="comment-init-bitpattern_-word"><div class="p">
    <p>Construct an <code>UnsafeMutablePointer</code> from a given address in memory.</p>

<p>This is a fundamentally unsafe conversion.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init(bitPattern: Word)</code>

    </div></div>
</div>
<div class="declaration" id="init-nilliteral_">
<a class="toggle-link" data-toggle="collapse" href="#comment-init-nilliteral_">init(<wbr>nilLiteral:)</a><div class="comment collapse" id="comment-init-nilliteral_"><div class="p">
    <p>Create an instance initialized with <code>nil</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init(nilLiteral: ())</code>

    </div></div>
</div>


<h3>Instance Variables</h3>
<div class="declaration" id="var-debugdescription_-string">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-debugdescription_-string">var debugDescription: String</a><div class="comment collapse" id="comment-var-debugdescription_-string"><div class="p">
    <p>A textual representation of <code>self</code>, suitable for debugging.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var debugDescription: String { get }</code>

    </div></div>
</div>
<div class="declaration" id="var-hashvalue_-int">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-hashvalue_-int">var hashValue: Int</a><div class="comment collapse" id="comment-var-hashvalue_-int"><div class="p">
    <p>The hash value.</p>

<p><strong>Axiom:</strong> <code>x == y</code> implies <code>x.hashValue == y.hashValue</code></p>

<p><strong>Note:</strong> the hash value is not guaranteed to be stable across
different invocations of the same program.  Do not persist the
hash value across program runs.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var hashValue: Int { get }</code>

    </div></div>
</div>
<div class="declaration" id="var-memory_-t">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-memory_-t">var memory: T</a><div class="comment collapse" id="comment-var-memory_-t"><div class="p">
    <p>Access the underlying raw memory, getting and
setting values.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var memory: T { get set }</code>

    </div></div>
</div>

<h3>Subscripts</h3>
<div class="declaration" id="subscript-subscript_-int">
<a class="toggle-link" data-toggle="collapse" href="#comment-subscript-subscript_-int">subscript(_: Int)</a>
<div class="comment collapse" id="comment-subscript-subscript_-int"><div class="p">
    

    <h4>Declaration</h4>    
    <code class="language-swift">subscript(i: Int) -&gt; T { get nonmutating set }</code>
    
    
</div></div>
</div>

<h3>Static Methods</h3>
<div class="declaration" id="func-alloc_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-alloc_">static func alloc(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-alloc_"><div class="p">
    <p>Allocate memory for <code>num</code> objects of type <code>T</code>.</p>

<p>Postcondition: the memory is allocated, but not initialized.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">static func alloc(num: Int) -&gt; UnsafeMutablePointer&lt;T&gt;</code>
    
    
</div></div>
</div>

<h3>Instance Methods</h3>
<div class="declaration" id="func-advancedby_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-advancedby_">func advancedBy(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-advancedby_"><div class="p">
    <p>Return <code>self</code> offset by <code>n</code> steps.</p>

<p><strong>Returns:</strong> If <code>n &gt; 0</code>, the result of applying <code>successor</code> to
<code>self</code> <code>n</code> times.  If <code>n &lt; 0</code>, the result of applying
<code>predecessor</code> to <code>self</code> <code>-n</code> times. Otherwise, <code>self</code>.</p>

<p>Complexity: O(1)</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func advancedBy(n: Int) -&gt; UnsafeMutablePointer&lt;T&gt;</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-assignbackwardfrom_count_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-assignbackwardfrom_count_">func assignBackwardFrom(<wbr>_:<wbr>count:)</a>
        
<div class="comment collapse" id="comment-func-assignbackwardfrom_count_"><div class="p">
    <p>Assign from <code>count</code> values beginning at <code>source</code> into
initialized memory, proceeding from the last value to the first.
Use this for assigning ranges into later memory that may overlap
with the source range.</p>

<p>Requires: either <code>source</code> precedes <code>self</code> or follows <code>self + count</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func assignBackwardFrom(source: UnsafeMutablePointer&lt;T&gt;, count: Int)</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-assignfrom_count_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-assignfrom_count_">func assignFrom(<wbr>_:<wbr>count:)</a>
        
<div class="comment collapse" id="comment-func-assignfrom_count_"><div class="p">
    <p>Assign from <code>count</code> values beginning at source into initialized
memory, proceeding from the first element to the last.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func assignFrom(source: UnsafeMutablePointer&lt;T&gt;, count: Int)</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-dealloc_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-dealloc_">func dealloc(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-dealloc_"><div class="p">
    <p>Deallocate <code>num</code> objects.</p>

<p><strong>num</strong> number of objects to deallocate.  Should match exactly
the value that was passed to <code>alloc()</code> (partial deallocations are not
possible).</p>

<p>Precondition: the memory is not initialized.</p>

<p>Postcondition: the memory has been deallocated.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func dealloc(num: Int)</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-destroy">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-destroy">func destroy()</a>
        
<div class="comment collapse" id="comment-func-destroy"><div class="p">
    <p>Destroy the object the pointer points to.</p>

<p>Precondition: the memory is initialized.</p>

<p>Postcondition: the value has been destroyed and the memory must
be initialized before being used again.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func destroy()</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-destroy_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-destroy_">func destroy(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-destroy_"><div class="p">
    <p>Destroy the <code>count</code> objects the pointer points to.
Precondition: the memory is initialized.</p>

<p>Postcondition: the value has been destroyed and the memory must
be initialized before being used again.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func destroy(count: Int)</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-distanceto_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-distanceto_">func distanceTo(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-distanceto_"><div class="p">
    <p>Return the minimum number of applications of <code>successor</code> or
<code>predecessor</code> required to reach <code>other</code> from <code>self</code>.</p>

<p>Complexity: O(1).</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func distanceTo(x: UnsafeMutablePointer&lt;T&gt;) -&gt; Int</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-encode">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-encode">func encode()</a>
        
<div class="comment collapse" id="comment-func-encode"><div class="p">
    <p>Transform <code>self</code> into a series of machine words that can be
appropriately interpreted by C varargs</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func encode() -&gt; [Word]</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-getmirror">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-getmirror">func getMirror()</a>
        
<div class="comment collapse" id="comment-func-getmirror"><div class="p">
    <p>Returns a mirror that reflects <code>self</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func getMirror() -&gt; MirrorType</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-initialize_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-initialize_">func initialize(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-initialize_"><div class="p">
    <p>Initialize the value the pointer points to, to construct
an object where there was no object previously stored.</p>

<p>Precondition: the memory is not initialized.</p>

<p>Postcondition: the memory is initalized; the value should eventually
be destroyed or moved from to avoid leaks.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func initialize(newvalue: T)</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-initializefrom_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-initializefrom_">func initializeFrom(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-initializefrom_"><div class="p">
    <p>Copy the elements of <code>C</code> into raw memory.</p>

<p>Precondition: the memory is not initialized.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func initializeFrom&lt;C : CollectionType where T == T&gt;(source: C)</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-initializefrom_count_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-initializefrom_count_">func initializeFrom(<wbr>_:<wbr>count:)</a>
        
<div class="comment collapse" id="comment-func-initializefrom_count_"><div class="p">
    <p>Copy count values beginning at source into raw memory.</p>

<p>Precondition: the memory is not initialized.</p>

<p>Requires: <code>self</code> and <code>source</code> may not overlap.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func initializeFrom(source: UnsafeMutablePointer&lt;T&gt;, count: Int)</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-move">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-move">func move()</a>
        
<div class="comment collapse" id="comment-func-move"><div class="p">
    <p>Retrieve the value the pointer points to, moving it away
from the location referenced in memory.</p>

<p>Equivalent to reading <code>memory</code> property and calling <code>destroy()</code>,
but more efficient.</p>

<p>Precondition: the memory is initialized.</p>

<p>Postcondition: the value has been destroyed and the memory must
be initialized before being used again.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func move() -&gt; T</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-moveassignfrom_count_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-moveassignfrom_count_">func moveAssignFrom(<wbr>_:<wbr>count:)</a>
        
<div class="comment collapse" id="comment-func-moveassignfrom_count_"><div class="p">
    <p>Assign from <code>count</code> values beginning at <code>source</code> into initialized
memory, transforming the source values into raw memory.</p>

<p>Requires: the <code>self</code> and <code>source</code> ranges may not overlap.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func moveAssignFrom(source: UnsafeMutablePointer&lt;T&gt;, count: Int)</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-moveinitializebackwardfrom_count_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-moveinitializebackwardfrom_count_">func moveInitializeBackwardFrom(<wbr>_:<wbr>count:)</a>
        
<div class="comment collapse" id="comment-func-moveinitializebackwardfrom_count_"><div class="p">
    <p>Move <code>count</code> values beginning at <code>source</code> into uninitialized memory,
transforming the source values into raw memory, proceeding from
the last value to the first.  Use this for copying ranges into
later memory that may overlap with the source range.</p>

<p>Requires: either <code>source</code> precedes <code>self</code> or follows <code>self + count</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func moveInitializeBackwardFrom(source: UnsafeMutablePointer&lt;T&gt;, count: Int)</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-moveinitializefrom_count_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-moveinitializefrom_count_">func moveInitializeFrom(<wbr>_:<wbr>count:)</a>
        
<div class="comment collapse" id="comment-func-moveinitializefrom_count_"><div class="p">
    <p>Move count values beginning at source into raw memory,
transforming the source values into raw memory.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func moveInitializeFrom(source: UnsafeMutablePointer&lt;T&gt;, count: Int)</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-predecessor">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-predecessor">func predecessor()</a>
        
<div class="comment collapse" id="comment-func-predecessor"><div class="p">
    <p>Returns the previous consecutive value before <code>self</code>.</p>

<p>Requires: the previous value is representable.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func predecessor() -&gt; UnsafeMutablePointer&lt;T&gt;</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-put_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-put_">mutating func put(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-put_"><div class="p">
    

    <h4>Declaration</h4>    
    <code class="language-swift">mutating func put(x: T)</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-successor">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-successor">func successor()</a>
        
<div class="comment collapse" id="comment-func-successor"><div class="p">
    <p>Returns the next consecutive value after <code>self</code>.</p>

<p>Requires: the next value is representable.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func successor() -&gt; UnsafeMutablePointer&lt;T&gt;</code>
    
    
</div></div>
</div>


